package com.example.seatastoragesys.api;

import com.example.seatastoragesys.domain.Storage;
import com.example.seatastoragesys.service.StorageService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

@RequestMapping("/api/storage")
@RestController
@Slf4j
public class StorageController {

    @Autowired
    private StorageService storageService;

    /**
     * 扣减 库存
     *
     * @param productId 产品id
     * @param count
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/deduct")
    public String deduct(@RequestParam Long productId, @RequestParam Integer count) throws SQLException {
        log.info("storage XID " + RootContext.getXID());
        storageService.deduct(productId, count);
        return "ok";
    }

    /**
     * 按库存id查询Storage
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/get/{id}")
    public Storage getById(@PathVariable("id") Long id) {
        return storageService.get(id);
    }

//    /**
//     * 批量更新库存
//     */
//    @GetMapping(value = "/batch/update")
//    public void batchUpdateCond() {
//        storageService.batchUpdate();
//    }
//
//    /**
//     * 批量删除库存
//     */
//    @GetMapping(value = "/batch/delete")
//    public void batchDeleteCond() {
//        storageService.batchDelete();
//    }

}